package com.yanqu.road.server.gameplayer.module.activity.luckyshop;

import com.yanqu.road.entity.activity.luckyshop.LuckyShopConfig;
import com.yanqu.road.entity.activity.luckyshop.LuckyShopUserData;
import com.yanqu.road.entity.activity.luckyshop.config.LuckyShopDrawConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.LuckyShopBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.activity.LuckyShopProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.luckyshop.LuckyShopMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * 幸运小铺
 */
public class LuckyShopModule extends GeneralModule {

    private LuckyShopUserData userData;

    public LuckyShopModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config != null && ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            userData = LuckyShopBussiness.getUserData(config.getActivityId(), player.getUserId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (userData != null) {
            if (userData.isInsertOption()) {
                LuckyShopBussiness.addLuckyShopUserData(userData);
            } else if (userData.isUpdateOption()) {
                LuckyShopBussiness.updateLuckyShopUserData(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        initSystem();
    }

    /**
     * 初始化
     */
    public void initSystem() {
        if (!SystemOpenMgr.systemOpen(player, eSystemId.LuckyShop.getValue())) {
            return;
        }
        syncConfig();
        initUserData();
        syncUserData();
    }

    private void syncUserData() {
        if (userData == null) {
            return;
        }
        LuckyShopProto.LuckyShopUserDataSyncMsg.Builder builder = LuckyShopProto.LuckyShopUserDataSyncMsg.newBuilder();
        builder.setUserData(parseLuckyShopUserDataMsg());
        player.sendPacket(ClientProtocol.U_LUCKY_SHOP_USER_DATA_SYNC, builder);
    }

    private void syncConfig() {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config == null) {
            return;
        }
        LuckyShopProto.LuckyShopConfigSyncMsg.Builder builder = LuckyShopProto.LuckyShopConfigSyncMsg.newBuilder();
        for (LuckyShopDrawConfig drawConfig : config.getDrawConfigMap().values()) {
            LuckyShopProto.LuckyShopDrawConfigTemp.Builder temp = LuckyShopProto.LuckyShopDrawConfigTemp.newBuilder();
            temp.setId(drawConfig.getId());
            temp.setReward(drawConfig.getReward());
            temp.setBig(drawConfig.getBig());
            temp.setWeight(drawConfig.getWeight());
            builder.addDrawConfig(temp);
        }
        player.sendPacket(ClientProtocol.U_LUCKY_SHOP_CONFIG_SYNC, builder);
    }

    private void initUserData() {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config == null || !ActivityHelper.activityInShowTime(config.getActivityInfo())) {
            return;
        }
        if (userData != null && userData.getActivityId() == config.getActivityId()) {
            return;
        }
        LuckyShopUserData tmpUserData = new LuckyShopUserData();
        tmpUserData.setActivityId(config.getActivityId());
        tmpUserData.setUserId(player.getUserId());
        tmpUserData.setRewardIdx(-1);
        tmpUserData.setDrawTimes(0);
        tmpUserData.setInsertOption();
        userData = tmpUserData;
    }

    /**
     * 自选奖励
     *
     * @param rewardIdx
     * @return
     */
    public int chooseReward(int rewardIdx) {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config == null) {
            return GameErrorCode.E_LUCKY_SHOP_CONFIG_NOT_FOUND;
        }
        if (userData == null) {
            return GameErrorCode.E_LUCKY_SHOP_USER_NOT_FOUND;
        }
        if (rewardIdx < 0 || rewardIdx >= config.getBigRewardList().size()) {
            return GameErrorCode.E_ERROR_PARAMETER;
        }
        userData.setRewardIdx(rewardIdx);
        LuckyShopProto.LuckyShopChooseRewardRespMsg.Builder respMsg = LuckyShopProto.LuckyShopChooseRewardRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setUserData(parseLuckyShopUserDataMsg());
        player.sendPacket(Protocol.U_LUCKY_SHOP_CHOOSE_REWARD, respMsg);
        return 0;
    }

    /**
     * 领大奖
     */
    public int getBigReward() {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config == null) {
            return GameErrorCode.E_LUCKY_SHOP_CONFIG_NOT_FOUND;
        }
        if (userData == null) {
            return GameErrorCode.E_LUCKY_SHOP_USER_NOT_FOUND;
        }
        if (userData.isReward()) {
            return GameErrorCode.E_LUCKY_SHOP_HAD_REWARD;
        }
        if (userData.getRewardIdx() == -1) {
            return GameErrorCode.E_LUCKY_SHOP_NOT_CHOOSE_REWARD;
        }
        // 判断一下进度
        if (userData.getDrawTimes() < config.getBigRewardTimes()) {
            return GameErrorCode.E_LUCKY_SHOP_BIG_REWARD_TIMES_NO_ENOUGH;
        }
        // 什么都不要管，先改状态
        userData.setReward(true);
        // 拿一下奖励
        Property reward = config.getBigRewardList().get(userData.getRewardIdx());
        // 获得大奖
        player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.LuckyShop, eLogMoneyType.LuckyShopGetBigReward);
        LuckyShopProto.LuckyShopGetBigRewardRespMsg.Builder builder = LuckyShopProto.LuckyShopGetBigRewardRespMsg.newBuilder();
        builder.setRet(0);
        builder.setReward(PropertyHelper.parsePropertyToString(reward));
        builder.setUserData(parseLuckyShopUserDataMsg());
        player.sendPacket(Protocol.U_LUCKY_SHOP_GET_BIG_REWARD, builder);
        return 0;
    }


    public LuckyShopProto.LuckyShopUserDataMsg.Builder parseLuckyShopUserDataMsg() {
        if (userData == null) {
            return null;
        }
        LuckyShopProto.LuckyShopUserDataMsg.Builder builder = LuckyShopProto.LuckyShopUserDataMsg.newBuilder();
        builder.setRewardIdx(userData.getRewardIdx());
        builder.setDrawTimes(userData.getDrawTimes());
        builder.setIsReward(userData.isReward());
        return builder;
    }

    /**
     * 开抽, 成功返回奖励id
     */
    public int draw() {
        LuckyShopConfig config = LuckyShopMgr.getConfig();
        if (config == null) {
            return 0;
        }
        int rewardId = LuckyShopMgr.randomRewardId();
        // 加一下进度
        userData.setDrawTimes(userData.getDrawTimes() + 1);
        // 更新进度
        player.notifyListener(eGamePlayerEventType.LuckyShopWatchTimes.getValue(), userData.getDrawTimes());
        return rewardId;
    }
}
